package com.ruoyi.common.utils.date;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.temporal.TemporalAdjusters;
import java.util.Calendar;
import java.util.Date;

public class DateUtils {
	/**
	 * 日期格式化
	 * @param date 日期对象
	 * @return 2018-01-01 01:01:01
	 */
	public static String dateFormatHour(Date date){
		if(null == date){
			return null;
		}
		SimpleDateFormat sdf = new SimpleDateFormat("MM月dd日 HH:mm");
		return sdf.format(date);
	}
	public static String format(Date date, String pattern) {
		if (date != null) {
			SimpleDateFormat df = new SimpleDateFormat(pattern);
			return df.format(date);
		}
		return null;
	}
	/**
	 * 日期格式化
	 * @param date 日期对象
	 * @return 2018-01-01 01:01:01
	 */
	public static String dateFormat(Date date){
		if(null == date){
			return null;
		}
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS");
		return sdf.format(date);
	}

	public static String getNowDate(){
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		return sdf.format(new Date());
	}

	/**
	 * 日期格式化
	 * @param date 日期对象
	 * @return 2018-01-01 01:01:01
	 */
	public static String dateFormatDir(Date date){
		if(null == date){
			return null;
		}
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
		return sdf.format(date);
	}

	/**
	 * 获取对象日期和当前日期比较后的描述性日期
	 * 刚刚,N分钟前,N小时前,N天前,N年前
	 *
	 */
	public static Integer getTimeCompareStringold(Date date) {
		Integer shortString = 2;
		long now = Calendar.getInstance().getTimeInMillis();
		long delTime = (now - date.getTime()) / 1000;
		if((int) (delTime / (60)) <= 10) {
			shortString = 1;
		}
		return shortString;
	}

	/** 调整用户在线时间显示
	 * paul 2021-03-16 14:11
	 * @param date
	 * @return
	 */
	public static String getTimeCompareString(Date date) {
		String shortString = null;
		long now = Calendar.getInstance().getTimeInMillis();
		long delTime = (now - date.getTime()) / 1000;
		if((int) (delTime / (60)) <= 10) {
			shortString = "活跃";
		}else {
			shortString ="";
		}
		return shortString;
	}

	public static String getTimeCompareStringDynamic(Date date) {
		String shortString = null;
		long now = Calendar.getInstance().getTimeInMillis();
		long delTime = (now - date.getTime()) / 1000;
		if (delTime > 24 * 60 * 60) {
			return format(date, "yyyy.MM.dd HH:mm");
			
		} else if (delTime > 60 * 60) {
			shortString = (int) (delTime / (60 * 60)) + "小时前";

		} else if (delTime > 60) {
			shortString = (int) (delTime / (60)) + "分钟前";

		} else  {
			shortString ="刚刚";
		}
		return shortString;
	}
	
	public static String getTimeCompareStringDynamicYmd(Date date) {
		String shortString = null;
		long now = Calendar.getInstance().getTimeInMillis();
		long delTime = (now - date.getTime()) / 1000;
		if (delTime > 24 * 60 * 60) {
			return format(date, "yyyy.MM.dd");
			
		} else if (delTime > 60 * 60) {
			shortString = (int) (delTime / (60 * 60)) + "小时前";

		} else if (delTime > 60) {
			shortString = (int) (delTime / (60)) + "分钟前";

		} else  {
			shortString ="刚刚";
		}
		return shortString;
	}
	
	/**
	 * 	我的足迹
	 * @param date
	 * @return
	 */
	public static String getTimeCompareStringFootprint(Date date) {
		String shortString = null;
		long now = Calendar.getInstance().getTimeInMillis();
		long delTime = (now - date.getTime()) / 1000;
		if (delTime > 7 * 24 * 60 * 60) {
			shortString = DateUtils.format(date, "MM月dd日");

		} else if (delTime > 24 * 60 * 60) {
			shortString = (int) (delTime / (24 * 60 * 60)) + "天前";

		} else if (delTime > 60 * 60) {
			shortString = (int) (delTime / (60 * 60)) + "小时前";

		} else if (delTime > 60) {
			shortString = (int) (delTime / (60)) + "分钟前";

		} else  {
			shortString ="刚刚";
		}
		return shortString;
	}

	public static String getLoginTime(Date date) {
		String shortString = null;
		long now = Calendar.getInstance().getTimeInMillis();
		long delTime = (now - date.getTime()) / 1000;
		if (delTime > 7 * 24 * 60 * 60) {
			shortString = "";
			
		}else if (delTime > 24 * 60 * 60) {
			shortString = "最近活跃";
			
		} else if (delTime > 60 * 60) {
			shortString = (int) (delTime / (60 * 60)) + "小时前活跃";

		} else if (delTime > 60) {
			shortString = (int) (delTime / (60)) + "分钟前活跃";

		} else  {
			shortString ="刚刚活跃";
		}
		return shortString;
	}
	public static String getLoginTimeHome(Date date) {
		String shortString = null;
		long now = Calendar.getInstance().getTimeInMillis();
		long delTime = (now - date.getTime()) / 1000;
		if (delTime > 30 * 24 * 60 * 60) {
			shortString = "";
			
		}else if (delTime > 24 * 60 * 60) {
			shortString = "最近活跃";

		} else if (delTime > 60 * 60) {
			shortString = (int) (delTime / (60 * 60)) + "小时前";

		} else if (delTime > 60) {
			shortString = (int) (delTime / (60)) + "分钟前";

		} else  {
			shortString ="在线";
		}
		return shortString;
	}
	/**
	 * 自动生成订单编号
	 * @return
	 */
	public static String generateOrderNo(Long userId ,Date date){
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
		return userId + sdf.format(date);
	}

	/**
	 * 自动生成订单编号
	 * @return
	 */
	public static String getOrderNo(Long id, Date date){
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
		return id + sdf.format(date);
	}

	/**
	 * 日期格式化
	 * @param date 日期对象
	 * @return 2018年01月01日
	 */
	public static String dateFormatDay(Date date){
		if(null == date){
			return null;
		}
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日");
		return sdf.format(date);
	}

	/**
	 * 根据生日计算年龄
	 */
	public static int getAgeByBirth(Date birthday) {
		int age = 0;
		try {
			Calendar now = Calendar.getInstance();
			now.setTime(new Date());// 当前时间

			Calendar birth = Calendar.getInstance();
			birth.setTime(birthday);

			if (birth.after(now)) {//如果传入的时间，在当前时间的后面，返回0岁
				age = 0;
			} else {
				age = now.get(Calendar.YEAR) - birth.get(Calendar.YEAR);
				if (now.get(Calendar.DAY_OF_YEAR) > birth.get(Calendar.DAY_OF_YEAR)) {
					age += 1;
				}
			}
			return age;
		} catch (Exception e) {//兼容性更强,异常后返回数据
			return 0;
		}
	}

	/**
	 * 当前时间所在月的第一天
	 */
	public static Date getStartDayTimeOfMonth(){
		LocalDate localDate = LocalDate.now();
		LocalDateTime localDateTime = localDate.atStartOfDay().with(TemporalAdjusters.firstDayOfMonth());
		return Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());
	}

	/**
	 *
	 * @param nowTime   当前时间
	 * @param startTime	开始时间
	 * @param endTime   结束时间
	 * @return
	 * @author sunran   判断当前时间在时间区间内
	 */
	public static boolean isEffectiveDate(Date nowTime, Date startTime, Date endTime) {
		if (nowTime.getTime() == startTime.getTime()
				|| nowTime.getTime() == endTime.getTime()) {
			return true;
		}

		Calendar date = Calendar.getInstance();
		date.setTime(nowTime);

		Calendar begin = Calendar.getInstance();
		begin.setTime(startTime);

		Calendar end = Calendar.getInstance();
		end.setTime(endTime);

		if (date.after(begin) && date.before(end)) {
			return true;
		} else {
			return false;
		}
	}


	/**
	 * 将日期字符串转化为Date类型
	 * @param dateStr
	 * @param pattern
	 * @return
	 */
	public static Date stringToDate(String dateStr, String pattern) {
		try {
			DateFormat sdf = new SimpleDateFormat(pattern);
			Date date = sdf.parse(dateStr);
			return date;
		} catch (Exception ex) {
			return null;
		}
	}


	/**
	 * 将日期转化为字符串类型
	 * @param date
	 * @param pattern
	 * @return
	 */
	public static String dateToString(Date date, String pattern) {
		DateFormat sdf = new SimpleDateFormat(pattern);
		String dateStr = sdf.format(date);
		return dateStr;
	}

	/**
	 * 当前日期是星期几
	 * @return
	 */
	public static int getDayOfweek(){
		Calendar c = Calendar.getInstance();
		int week = c.get(Calendar.DAY_OF_WEEK);//1--7的值,对应：星期日，星期一，星期二，星期三....星期六
		return week;
	}

	public static Date addDateForMonths(Date date,int months) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);//设置起时间
		cal.add(Calendar.MONTH, months);//增加一个月   
		return cal.getTime();
	}   

	public static Date addDateForDays(Date date,int days) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);//设置起时间
		cal.add(Calendar.DATE, days);//天
		return cal.getTime();
	}
	
	public static Date addDateForMinutes(Date date,int minutes) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);//设置起时间
		cal.add(Calendar.MINUTE, minutes);//增加分钟
		return cal.getTime();
	}

	//计算redis存活时间
	public static long countDayTime(Date date1,Date date2) {
		return (date2.getTime()-date1.getTime());
	}
	//判断是否过期true没过期，false 过期
	public static boolean isOverdue(Date date1,Date date2) {
		Long date =(date2.getTime()-date1.getTime());
		if(date>0) {
			return true;
		}else {
			return false;
		}
	}

	/**
	 * 	指定时间减几天或者加几天
	 * @param past	天数
	 * @param date	指定时间
	 * @param type	1=减 2=加
	 * @return
	 */
	public static String getPastDate(int past,Date date,int type) { // type 1=减 2=加
		Calendar calendar = Calendar.getInstance();
		if(null != date) {
			calendar.setTime(date);
		}
		if(1 == type) {
			calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR) - past);
		}else {
			calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR) + past);
		}
		Date today = calendar.getTime();
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		String result = format.format(today);
		return result;
	}



	/**
	 * 	指定时间减几天或者加几天
	 * @param past	天数
	 * @param date	指定时间
	 * @param type	1=减 2=加
	 * @return
	 */
	public static Date getPastDateTime(int past,Date date,int type) { // type 1=减 2=加
		Calendar calendar = Calendar.getInstance();
		if(null != date) {
			calendar.setTime(date);
		}
		if(1 == type) {
			calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR) - past);
		}else {
			calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR) + past);
		}

		Date today = calendar.getTime();
		return today;
	}


	/**
	 * 	指定时间减几年或者加几年
	 * @param past	年
	 * @param date	指定时间
	 * @param type	1=减 2=加
	 * @return
	 */
	public static Date getPastDateTimeYear(int past,Date date,int type) { // type 1=减 2=加
		Calendar calendar = Calendar.getInstance();
		if(null != date) {
			calendar.setTime(date);
		}
		if(1 == type) {
			calendar.set(Calendar.YEAR, calendar.get(Calendar.YEAR) - past);
		}else {
			calendar.set(Calendar.YEAR, calendar.get(Calendar.YEAR) + past);
		}

		Date today = calendar.getTime();
		return today;
	}

	/**
	 * 	两个时间相差的秒数
	 * @param date1
	 * @param date2
	 * @return
	 */
	public static Long getDaySecond(Date date1,Date date2){
		// 获取日期
		return  (date2.getTime() - date1.getTime()) / (1000L);
	}
	/**
	 * 	获取两个时间相差的月数
	 * @param start
	 * @param end
	 * @return
	 */
	public static int getMonth(Date start, Date end) {
		if (start.after(end)) {
			Date t = start;
			start = end;
			end = t;
		}
		Calendar startCalendar = Calendar.getInstance();
		startCalendar.setTime(start);
		Calendar endCalendar = Calendar.getInstance();
		endCalendar.setTime(end);
		Calendar temp = Calendar.getInstance();
		temp.setTime(end);
		temp.add(Calendar.DATE, 1);

		int year = endCalendar.get(Calendar.YEAR)
				- startCalendar.get(Calendar.YEAR);
		int month = endCalendar.get(Calendar.MONTH)
				- startCalendar.get(Calendar.MONTH);

		if ((startCalendar.get(Calendar.DATE) == 1)
				&& (temp.get(Calendar.DATE) == 1)) {
			return year * 12 + month + 1;
		} else if ((startCalendar.get(Calendar.DATE) != 1)
				&& (temp.get(Calendar.DATE) == 1)) {
			return year * 12 + month;
		} else if ((startCalendar.get(Calendar.DATE) == 1)
				&& (temp.get(Calendar.DATE) != 1)) {
			return year * 12 + month;
		} else {
			return (year * 12 + month - 1) < 0 ? 0 : (year * 12 + month);
		}
	}

	/**
	 * 	增加分钟
	 * @param date
	 * @param minute
	 * @return
	 */
	public static Date addMinute(Date date,int minute) {
		Calendar cal = Calendar.getInstance();  
		cal.setTime(date);  
		// 24小时制  
		cal.add(Calendar.MINUTE, minute);  
		return cal.getTime();  
	}
	
	public static Date addSecondDate(Date date,int second) {
		Calendar cal = Calendar.getInstance();  
		cal.setTime(date);  
		// 24小时制  
		cal.add(Calendar.SECOND, second);  
		return cal.getTime();  
	}

	/**
	 * 	增加秒
	 * @param date
	 * @param minute
	 * @return
	 */
	public static String addSecond(Date date,int second) {
		Calendar cal = Calendar.getInstance();  
		cal.setTime(date);  
		// 24小时制  
		cal.add(Calendar.SECOND, second); 
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		return sdf.format(cal.getTime());  
	}
	
	public static Date addHour(Date date,int second) {
		Calendar cal = Calendar.getInstance();  
		cal.setTime(date);  
		// 24小时制  
		cal.add(Calendar.HOUR, second); 
		return cal.getTime();  
	}
	

	/**
     * 	获取当前月第一天
     * @param month
     * @return
     */
    public static String getFirstDayOfMonth() {
        Calendar calendar = Calendar.getInstance();
        // 获取某月最小天数
        int firstDay = calendar.getActualMinimum(Calendar.DAY_OF_MONTH);
        // 设置日历中月份的最小天数
        calendar.set(Calendar.DAY_OF_MONTH, firstDay);
        // 格式化日期
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String firstDays = sdf.format(calendar.getTime());
        return firstDays;
    }
    /**
     * 	获取当前月最后一天
     * @param month
     * @return
     */
    public static String getLastDayOfMonth() {
        Calendar calendar = Calendar.getInstance();
        // 设置月份
//        calendar.set(Calendar.MONTH, month - 1);
        // 获取某月最大天数
        int lastDay=0;
        //2月的平年瑞年天数
        lastDay = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
        // 设置日历中月份的最大天数
        calendar.set(Calendar.DAY_OF_MONTH, lastDay);
        // 格式化日期
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String lastDays = sdf.format(calendar.getTime());
        return lastDays;
    }
    
    /**
     * 	获取当前时间距离凌晨的秒数
     * @return
     */
    public static long getMorningTime() {
    	Calendar cal = Calendar.getInstance();
	    cal.add(Calendar.DAY_OF_YEAR, 1);
	    cal.set(Calendar.HOUR_OF_DAY, 0);
	    cal.set(Calendar.SECOND, 0);
	    cal.set(Calendar.MINUTE, 0);
	    cal.set(Calendar.MILLISECOND, 0);
	    return (cal.getTimeInMillis() - System.currentTimeMillis()) / 1000;
    }
    
    /**
     * 	时间戳转String
     * @param timestamp
     * @return
     */
    public static String timestampToString(long timestamp) {
    	Date date = new Date(timestamp); // 将时间戳转换为Date对象

		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); // 指定日期格式

		String formattedDate = sdf.format(date); // 格式化Date对象为字符串

		return formattedDate; // 输出格式化后的日期字符串
    }

    public static Date timestampToDate(long timestamp) {
    	Date date = new Date(timestamp); // 将时间戳转换为Date对象
    	return date;
    }

    
	public static void main(String[] args) throws ParseException {
		//System.out.println(addMinute(new Date(), -5));
//		Calendar cal = Calendar.getInstance();  
//		cal.setTime(new Date());  
//		// 24小时制  
//		cal.add(Calendar.HOUR, 1);  
//		System.out.println(cal.getTime());
//		long time = 1723712744562L;
//		Date timestampToDate = timestampToDate(time);
//		System.out.println(dateFormat(timestampToDate));
		String pattern = "HH:mm";
		String beg = "09:00";
		String end = "15:02";
		String now = dateToString(new Date(), pattern);
		System.out.println(isEffectiveDate(stringToDate(now, pattern), stringToDate(beg, pattern), stringToDate(end, pattern)));
	}
}
